<?php
    // +----------------------------------------------------------------------
    // | 图片文件管理
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------
    namespace app\controller\file\admin;

    use app\common\event\Setting as SettingEvent;
    use app\common\event\SystemConfig as SystemConfigEvent;
    use app\common\model\SystemConfig as SystemConfigModel;
    use app\event\file\Image as ImageEvent;
    use app\model\file\Image as ImageModel;
    use yicmf\builder\Table;
    use think\facade\Url;
    use yicmf\builder\Edit;
    use app\controller\admin\Common;
    use think\facade\Config;
    use think\facade\Env;

    class Image extends Common
    {
        protected function initialize()
        {
            parent::initialize();
            $this->model = new ImageModel();
        }


        public function index()
        {
			$statusOptions = [[
				'id' => -1,
				'value' => '回收站'
			], [
				'id' => 1,
				'value' => '可用'
			]];
            // 显示页面
            $builder = new Table();
            $builder->model(ImageModel::class)
                ->order('id DESC')
                ->setSearchPostUrl('/file/admin/image/index', '')
                ->searchText('id', '图片ID', '图片的ID')
//                ->searchSelect('status', '状态', $statusOptions)
//                ->suggest((0 == Config::get('upload_image.recycle_day')) ? '当前配置回收站资源不会自动删除' : '回收站的资源保留' . Config::get('upload_image.recycle_day') . '天，之后彻底删除')
                ->searchDateTimeRange('create_time', '创建时间：')
                ->buttonCustom(url('/file/admin/image/setting'), '图片配置')
                //                ->buttonCustom(url('file/admin/Picture/driver'), '图片驱动')
                ->keyId()
                ->keyImage('url', '预览')
                ->keyText('url', '保存路径')
                ->keyText('driver', '储存')
                ->keyTime('create_time', '创建时间')
                ->keyUser('user_id', '执行者')
                ->keyStatus()
                ->actionDelete( )
                ->actionRemove();
            return $builder->fetch();
        }


        /**
         * 删除信息
         * 改变status信息删除.
         */
        public function delete()
        {
            $event = new ImageEvent();
            $result = $event->delete($this->request->get(), $this->user, true);
            if (0 == $result['code']) {
                $this->_returnSuccess( '删除成功');
            } else {
                $this->_returnError( '失败，失败信息：' . $this->model->getError());
            }
        }

        /**
         * 图片配置
         */
        public function setting()
        {

            if ($this->request->isPost()) {
                $event = new SystemConfigEvent();
                $re = $event->updateValue($this->request->post(),$this->user);
                $this->_returnAuto($re,false);
            } else {
                $set = SystemConfigModel::where('status', 1)->where('group', 'upload_image')->where('is_dev',0)->order('sort ASC')->select();


                    $explain[] ='开启压缩为上传控件的压缩';
                    $explain[] ='使用文字水印，需要确认字体文件是否存在';
                    $explain[] ='开启之后会将图片处理为宽度60,240,360,1280，单位:像素(px)';
                return SystemConfigEvent::builderView($set,'图片配置',$explain);
            }
//
//            if ($this->request->isPost()) {
//
//                $config = Config::get('upload_image');
//                $config = array_merge($config, $this->request->post());
//                // 配置文件模版
//                $config_tpl = file_get_contents(app()->getRootPath() . 'data' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'upload_image.tpl');
//                //替换配置项
//                foreach ($config as $name => $value) {
//                    $config_tpl = str_replace('{$' . $name . '}', $value, $config_tpl);
//                }
//                // 写入配置
//                file_put_contents(app()->getRootPath() . 'config' . DIRECTORY_SEPARATOR . 'upload_image.php', $config_tpl);
//                $this->_returnSuccess( '配置已经保存', true);
//            } else {
//                $data = Config::get('upload_image');
//                //                $data['has_compress'] = 0;
//                //                $data['has_water'] = 0;
//                $data['water_type'] = 'none';
//                $location = ['1' => '左上', '2' => '中上', '3' => '右上', '4' => '左中', '5' => '中间', '6' => '右中', '7' => '左下', '8' => '中下', '9' => '右下'];
//                // 显示页面
//                $builder = new Edit();
//                $builder->title('图片配置')
//                    //                    ->keyBool('has_zoom', '开启缩放', '开启之后可以节省空间')
//                    ->keyBool('has_original', '是否保留原图', '在开启缩放或者添加水印有效')
//                    //                    ->keyBool('has_compress', '是否开启图片压缩', '开启上传压缩')
//                    ->keyRadio('water_type', '水印类型', ['none' => '无水印', 'image' => '图片水印', 'text' => '文字水印'])
//                    ->keyRadio('location_id', '水印位置', $location)
//                    ->keyImageModel('water_image', '水印图片')
//                    ->keyText('text_content', '文字内容', '需要配置字体文件')
//                    ->keyNumber('text_size', '字体字号', '单位是像素')
//                    ->keyColor('text_color', '字体颜色')
//                    ->group('基础', 'has_zoom,has_original,water_type,location_id,water_image,text_content,text_size,text_color,recycle_day')
//                    ->group('高级', 'water_dy,water_dx,text_angle,water_mini_width,water_transparency')
//                    ->keyNumber('water_dx', '横轴边距', '单位:像素(px)，默认值为10')
//                    ->keyNumber('water_dy', '纵轴边距', '单位:像素(px)，默认值为10')
//                    ->keyNumber('text_angle', '文字倾斜角度')
//                    ->keyNumber('water_mini_width', '加水印图片宽度', '单位像素，低于这个宽度不加水印')
//                    ->keyNumber('water_transparency', '图片透明度', '0~100，默认值是100')
//                    ->keyNumber('recycle_day', '回收站保留天数', '0为永久保存')
//                    //                    ->keySelect('font_file', '字体文件', $location)
//                    ->setTrigger('water_type', 'none', '')
//                    ->setTrigger('water_type', 'image', 'location_id,water_image,water_transparency,water_mini_width')
//                    ->setTrigger('water_type', 'text', 'location_id,water_mini_width,text_content,text_size,text_color,text_offset,text_angle,font_file')
//                    ->explain('开启压缩为上传控件的压缩')
//                    ->explain('使用文字水印，需要确认字体文件是否存在')
//                    ->explain('开启之后会将图片处理为宽度60,240,360,1280，单位:像素(px)')
//                    ->data($data)
//                    ->buttonSubmit()
//                    ->buttonClose();
//                return $builder->fetch();
//            }
            //
        }

        /**
         * 图片驱动
         */
        public function driver()
        {
            if ($this->request->isPost()) {

				SettingEvent::update($this->request->post(), 'upload_image');
//                $config = Config::get('upload_image.');
//                $config = array_merge($config, $this->request->post());
//                // 配置文件模版
//                //替换配置项
//                foreach ($config as $name => $value) {
//                    $config_tpl = str_replace('{$' . $name . '}', $value, $config_tpl);
//                }
//                // 写入配置
//                file_put_contents(Env::get('app_path') . 'file' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'upload_image.php', $config_tpl);
                $this->_returnSuccess( '配置已经保存', true);
            } else {
                $data = Config::get('upload_image');
                // 显示页面
                $builder = new Edit();
                $builder->title('图片驱动')
                    ->keyText('mimes', '允许上传的mimes类型', '一般无需修改')
                    ->keyText('ext', '允许上传的文件后缀', '一般无需修改')
                    ->keyNumber('max_size', '上传大小限制', '单位M')
                    ->keyRadio('driver', '储存驱动', ImageModel::$driver_lists)
                    ->keyText('qiniu_secrect_key', '七牛secrectKey', '请到<a href="https://s.qiniu.com/ZfmaIr" target="_blank">www.qiniu.com</a>注册')
                    ->keyText('qiniu_access_key', '七牛accessKey', '请到<a href="https://s.qiniu.com/ZfmaIr" target="_blank">www.qiniu.com</a>注册')
                    ->keyText('qiniu_domain', '七牛domain', '注册的域名')
                    ->keyText('qiniu_bucket', '七牛bucket', '注册的空间')
                    ->keyHidden('qiniu_timeout')
                    ->setTrigger('driver', 'local', '')
                    ->setTrigger('driver', 'qiniu', 'qiniu_secrect_key,qiniu_access_key,qiniu_domain,qiniu_bucket,qiniu_timeout')
                    //                    ->explain('开启压缩为上传控件的压缩')
                    ->explain('如果使用七牛驱动，需要<a href="https://s.qiniu.com/ZfmaIr" target="_blank">注册</a>相关账号')
                    //                    ->explain('使用文字水印，需要确认字体文件是否存在')
                    ->data($data)
                    ->buttonSubmit()
                    ->buttonClose();
                return $builder->fetch();
            }
            //
        }
    }
